/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.wsgi.context;

import static cc.aileron.wsgi.context.HttpThreadLocalContext.localContext;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Provider;

/**
 * provider : wsgi-process-context
 * @author Aileron
 *
 */
public class ContextProviderProcess implements Provider<WsgiProcessContext>
{
    public WsgiProcessContext get()
    {
        return new WsgiProcessContext()
        {
            @Override
            public HttpServletRequest getRequest()
            {
                return localContext.get().request;
            }

            @Override
            public HttpServletResponse getResponse()
            {
                return localContext.get().response;
            }

            @Override
            public String getQueryString()
            {
                return localContext.get().request.getQueryString();
            }

            @Override
            public String getRequestURL()
            {
                return localContext.get().request.getRequestURL().toString();
            }

            @SuppressWarnings("unchecked")
            @Override
            public Map<String, Object> getParameterMap(final String encoding)
            {
                try
                {
                    final HttpParameterMap result = new HttpParameterMap();
                    final Enumeration<String> enumeration = 
                        localContext.get().request.getParameterNames();
                    while(enumeration.hasMoreElements())
                    {
                        final String key = enumeration.nextElement();
                        final ArrayList<String> arrayList = new ArrayList<String>();
                        final String [] parameters = localContext.get().request.getParameterValues(key);
                        if (parameters!=null) for(final String v : parameters)
                        {
                            final String v2 = new String( v.getBytes("ISO-8859-1"), encoding ); 
                            arrayList.add( v2 ); 
                        }
                        if(arrayList.size()==0)
                        {
                            continue;
                        }
                        if(arrayList.size()==1)
                        {
                            result.put(key, arrayList.get(0));
                            continue;
                        }
                        result.put(key, arrayList.toArray());
                    }
                    return result ;
                }
                catch(Exception e)
                {
                    throw new RuntimeException(e);
                }
            }
        };
    }
}